import ast
import threading
# from multiprocessing import Process

from django.db.models import Q

from movie.models import CollectMovieDB
from user.models import UserTag
from . import user_api
from phone import Phone

User = user_api.User()
add_user_tag = User.add_user_tag
modify_user_tag = User.modify_user_tag


# 线程延迟工作
def delay_work(time, work):
    threading.Timer(time, work).start()


# 获取电影标签
def get_movie_tag(movie_id):
    res_tag = []
    movie_data_tags = CollectMovieDB.objects.filter(movie_id=movie_id).values("tags", "genres", "countries",
                                                                              "languages", "year").first()
    # print(movie_data_tags)
    # tags = movie_data_tags["tags"]
    genres = movie_data_tags["genres"]
    countries = movie_data_tags["countries"]
    languages = movie_data_tags["languages"]
    year = movie_data_tags["year"]

    if genres:
        genres = ast.literal_eval(genres)
        for genre in genres:
            res_tag.append("info_movie_type--"+genre)
            # res_tag.append(genre)
    if countries:
        countries = ast.literal_eval(countries)
        for country in countries:
            res_tag.append("info_movie_tag--"+country)
            # res_tag.append(country)
    if languages:
        languages = ast.literal_eval(languages)
        for language in languages:
            res_tag.append("info_movie_tag--"+language)
            # res_tag.append(language)
    if year:
        res_tag.append("info_movie_tag--"+str(year))
        # res_tag.append(year)
    # print(res_tag)
    return res_tag



# 用户收藏操作打标签
def user_like_tag(key):

    key = ast.literal_eval(key)
    movie_tags = get_movie_tag(key["movie_id"])
    # movie_tags = key["movie_tags"]

    if key["tag_sign"] == "init":  # 电影第一次收藏
        for movie_tag in movie_tags:
            movie_tag = movie_tag.split("--")
            modify_user_tag(key["user_id"], movie_tag[0], movie_tag[1], "+3")

    elif key["tag_sign"] == "cancel":  # 电影取消收藏
        for movie_tag in movie_tags:
            movie_tag = movie_tag.split("--")
            modify_user_tag(key["user_id"], movie_tag[0], movie_tag[1], "-3")


# 用户评分操作打标签
def user_rating_tag(key):

    key = ast.literal_eval(key)
    movie_tags = get_movie_tag(key["movie_id"])
    # movie_tags = key["movie_tags"]

    if key["tag_sign"] == "init":  # 电影第一次评分
        for movie_tag in movie_tags:
            movie_tag = movie_tag.split("--")
            modify_user_tag(key["user_id"], movie_tag[0], movie_tag[1], "+" + str(key["rating"]))

    elif key["tag_sign"] == "again":  # 电影再次评分
        rating = str(key["rating"] - key["old_rating"])
        for movie_tag in movie_tags:
            movie_tag = movie_tag.split("--")
            modify_user_tag(key["user_id"], movie_tag[0], movie_tag[1], rating)


# 用户评论操作打标签
def user_comment_tag(key):

    key = ast.literal_eval(key)
    movie_tags = get_movie_tag(key["movie_id"])
    emotion = int(int(key["emotion"])/10)-5
    if key["tag_sign"] == "add":  # 添加电影评论
        for movie_tag in movie_tags:
            movie_tag = movie_tag.split("--")
            modify_user_tag(key["user_id"], movie_tag[0], movie_tag[1], str(emotion))

    elif key["tag_sign"] == "delete":  # 删除电影评论
        for movie_tag in movie_tags:
            movie_tag = movie_tag.split("--")
            modify_user_tag(key["user_id"], movie_tag[0], movie_tag[1], str(-emotion))


# 用户浏览操作打标签
def user_brow_tag(key):

    key = ast.literal_eval(key)
    movie_tags = get_movie_tag(key["movie_id"])
    # movie_tags = key["movie_tags"]
    for movie_tag in movie_tags:
        movie_tag = movie_tag.split("--")
        modify_user_tag(key["user_id"], movie_tag[0], movie_tag[1], "+5")

""" 这里定义了一个线程的类，等着被启动，启动后要干什么取决于传入什么函数，
函数所需的参数也是靠实例化时传入 """
class TagThread(threading.Thread):
    def __init__(self, func, args):
        self.func = func
        self.kwargs = args
        threading.Thread.__init__(self)

    def run(self):
        eval(str(self.func)+'("'+str(self.kwargs)+'")')


def tag_thread_work(add_type, **kwargs):
    # 这里定义了上面的所有函数名
    allow_type = ["user_info_tag", "user_like_tag", "user_rating_tag", "user_comment_tag", "user_brow_tag",
                  "user_search_tag"]
    # 传入函数名和若干个参数，然后启动线程
    if add_type in allow_type:
        TagThread(func=add_type, args=kwargs).start()

